<template>
  <el-drawer
    :visible.sync="$store.state.process.copyerDrawer"
    :with-header="false"
    :before-close="beforeClose"
    size="648px">
    <div class="title">
      <Title title="抄送设置" />
      <i class="icon el-icon-close" @click="beforeClose()" />
    </div>
    <div class="content">
      <SelectRoleUser :show-role-name="false" v-model="examineRoles" :type="type" :error-roles-ids="errorRolesIds" btn-text="添加抄送人" @removeError="removeError" />
      <div class="notice-template">
        <div class="notice-template-item center">
          <div class="label">自定义通知模板</div>
          <div class="val"><el-switch v-model="noticeTemplate.isOpen" class="fb-switch" @change="removeError" /></div>
        </div>
        <div v-if="noticeTemplate.isOpen" :class="{'is-error': noticeTemplateError}" class="notice-template-item">
          <div class="label"><span class="required">*</span>通知标题</div>
          <div class="val">
            <el-input
              v-model="noticeTemplate.title"
              :maxlength="50"
              class="width240"
              type="text"
              placeholder="请输入"
              size="small"
              @change="removeError"/>
            <div v-if="noticeTemplateError" class="err-msg">请输入通知标题</div>
          </div>
        </div>
        <div v-if="noticeTemplate.isOpen" class="notice-template-item">
          <div class="label">通知内容</div>
          <div class="val"> <el-input v-model="noticeTemplate.content" :maxlength="200" class="width240" type="textarea" resize="none" placeholder="请输入" size="small"/> </div>
        </div>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import { mapState } from 'vuex';
import Title from '@/components/Title';
import SelectRoleUser from './selectRoleUser';
import ElMessage from '@/utils/message';
import { ROLE_TYPE } from '@/utils/const/process';
import formMakeMixins from '@/views/legal/process/mixins/formMake';
export default {
  components: { Title, SelectRoleUser },
  mixins: [formMakeMixins],
  props: {
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      errorRolesIds: [],
      noticeTemplateError: false
    };
  },
  computed: {
    ...mapState({
      'copyerDrawerData': state => state.process.copyerDrawerData
    }),
    examineRoles() {
      if (this.copyerDrawerData && this.copyerDrawerData.examineRoles) {
        return this.clearDeletedData(this.copyerDrawerData.examineRoles);
      }
      return [];
    },
    noticeTemplate() {
      if (this.copyerDrawerData && this.copyerDrawerData.noticeTemplate) {
        return this.copyerDrawerData.noticeTemplate;
      }
      return {};
    }
  },
  methods: {
    beforeClose(done) {
      this.handelError();
      if (this.errorRolesIds.length || this.noticeTemplateError) {
        ElMessage.error('请检查信息是否填写完整');
        return;
      }
      if (!this.noticeTemplate.isOpen && (this.noticeTemplate.title || this.noticeTemplate.content)) {
        this.noticeTemplate.title = '';
        this.noticeTemplate.content = '';
      }
      this.$store.commit('SET_COPYER_SWITCH', false);
      this.$store.commit('SET_COPYER_DATA', {});
      done && done();
    },
    handelError() {
      this.errorRolesIds = [];
      this.examineRoles.forEach((i, index) => {
        if (i.roleType === ROLE_TYPE.DEPARTMENT && (!i.departmentIdList || i.departmentIdList.length === 0)) {
          this.errorRolesIds.push({
            type: 'dept', id: index, left: 172 + 'px', msg: '请选择部门'
          });
        } else if (i.roleType === ROLE_TYPE.CUSTOMIZE && (!i.diyUserIdList || i.diyUserList.length === 0)) {
          this.errorRolesIds.push({
            type: 'diyUser', id: index, left: 172 + 'px', msg: '请选择人员'
          });
        } else if (i.roleType === ROLE_TYPE.DIRECTOR && !i.manageRole && i.manageRole !== 0) {
          this.errorRolesIds.push({
            type: 'manage', id: index, left: 172 + 'px', msg: '请选择主管类型'
          });
        } else if (i.roleType === ROLE_TYPE.DESIGNATE && !i.designateUserSelect) {
          this.errorRolesIds.push({
            type: ROLE_TYPE.DESIGNATE, id: index, left: 172 + 'px', msg: '请选择人员选择字段'
          });
        } else if (i.roleType === ROLE_TYPE.ROLE_GROUP && !i.roleGroup) {
          this.errorRolesIds.push({
            type: ROLE_TYPE.ROLE_GROUP, id: index, left: 172 + 'px', msg: '请选择角色组'
          });
        } else if (i.roleType === ROLE_TYPE.BP_APPROVER) {
          if (!i.bpNames || !i.bpNames.length) {
            this.errorRolesIds.push({
              type: ROLE_TYPE.BP_APPROVER, id: index, left: 172 + 'px', msg: '请选择BP类型'
            });
          } else if (!i.bpDepts || !i.bpDepts.length) {
            this.errorRolesIds.push({
              type: ROLE_TYPE.BP_APPROVER, id: index, left: 172 + 150 + 'px', msg: '请选择部门'
            });
          }
        } else if (i.roleType === ROLE_TYPE.ARREARS_USERS && !i.transferRole && i.transferRole !== 0) {
          this.errorRolesIds.push({
            type: ROLE_TYPE.ARREARS_USERS, id: index, left: 172 + 'px', msg: '请选择指定人员字段'
          });
        } else if (i.roleType === ROLE_TYPE.DESIGNATE_DIRECTOR) {
          if (!i.designateDirectorUser) {
            this.errorRolesIds.push({
              type: ROLE_TYPE.DESIGNATE_DIRECTOR, id: index, left: 172 + 'px', msg: '请选择人员选择字段'
            });
          } else if (typeof i.designateDirector !== 'number') {
            this.errorRolesIds.push({
              type: ROLE_TYPE.DESIGNATE_DIRECTOR, id: index, left: 172 + 150 + 'px', msg: '请选择主管类型'
            });
          }
        }
      });
      if (this.noticeTemplate.isOpen && !this.noticeTemplate.title) {
        this.noticeTemplateError = true;
      }
    },
    removeError() {
      this.errorRolesIds = [];
      this.noticeTemplateError = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.content {
  padding: 17px 24px;
  &::v-deep {
    .el-checkbox__label {
      padding-left: 4px;
    }
  }
}
.title {
  display: flex;
  height: 50px;
  padding: 0 24px;
  align-items: center;
  border-bottom: 1px solid #f5f5f5;
  .icon {
    font-size: 18px;
    color: #999;
    cursor: pointer;
  }
}
::v-deep {
  .el-drawer__body {
    @include scrollBar($x: hidden, $y: scroll);
  }
}
.notice-template {
  margin-top: 16px;
  .notice-template-item {
    display: flex;
    margin-bottom: 16px;
    &.center {
      align-items: center;
    }
    .val {
      position: relative;
    }
    .label {
      font-size: 14px;
      color: #666;
      margin-right: 12px;
      text-align: right;
      width: 100px;
      height: 32px;
      line-height: 32px;
    }
    .required {
      color: #F56C6C;
      margin-right: 4px;
    }
  }
}
.is-error {
  // #f56c6c
  &::v-deep {
    .el-input__inner {
      border-color: #f56c6c;
    }
  }
  .err-msg{
    position: absolute;
    left: 0;
    color: #f56c6c;
    font-size: 12px;
  }
}
.width240 {
  width: 240px;
}
</style>
